﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Logic.UI;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Timeline
{
    public sealed class UIControlPositionTrack : BaseTrack
    {
        [XmlElement("X")]
        public string x = "";

        [XmlElement("Y")]
        public string y = "";

        private Curve curveX = null;
        private Curve curveY = null;

        public UIControlPositionTrack() { }

        public UIControlPositionTrack(string name) : base(name) { }

        protected override bool OnLoad(GraphicsDevice graphics, ContentManager contentManager)
        {
            curveX = LoadCurve(contentManager, x);
            curveY = LoadCurve(contentManager, y);

            return base.OnLoad(graphics, contentManager);
        }

        public override void SortKeys() { }

        public override bool Update(float time)
        {
            if (curveX == null || curveY == null)
            {
                return false;
            }

            XUIPlaceableControl control = this.target as XUIPlaceableControl;
            if (control != null)
            {
                float x = curveX.Evaluate(time);
                float y = curveY.Evaluate(time);
                control.SetPosition(x, y);
                return true;
            }

            return false;
        }
    }
}
